package com.google.appengine.preferencessync.internal;

import com.google.api.client.googleapis.services.GoogleClient;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;

/**
 * Common utilities.
 */
public class CloudEndpointUtils {

  public static final boolean LOCAL_ANDROID_RUN = false;
  private static final String LOCAL_APP_ENGINE_SERVER_URL = "http://10.0.2.2:8888";

  /**
   * Updates the Google client builder to connect the appropriate
server based on
   * whether LOCAL_ANDROID_RUN is true or false.
   *
   * @param builder Google client builder
   * @return same Google client builder
   */
  public static <B extends GoogleClient.Builder> B updateBuilder(B builder) {
    if (LOCAL_ANDROID_RUN) {
      builder.setRootUrl(LOCAL_APP_ENGINE_SERVER_URL + "/_ah/api/");
    }
    // only enable GZip when connecting to remote server
    final boolean enableGZip = builder.getRootUrl().startsWith("https:");
    builder.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
      public void initialize(JsonHttpRequest jsonHttpRequest) {
        jsonHttpRequest.setEnableGZipContent(enableGZip);
      }
    });
    return builder;
  }
}
